﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;
using System.Data.Services.Common;
using System.Data.Services.Client;
using System.Data.Services.Providers;
using System.Data.Services.Internal;

namespace DataServicesWebApp
{
    [System.ServiceModel.ServiceBehavior(
       IncludeExceptionDetailInFaults = true)]
    public class AdventureWorks : DataService<FotbalEntities>
    {
        /// <summary>
        /// This method is called only once to initialize service-wide policies.
        /// </summary>
        public static void InitializeService(DataServiceConfiguration config)
        {
            // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
            // Examples:
            // config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.AllRead);
            // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);

            // For testing purposes use "*" to indicate all entity sets/service operations.
            // "*" should NOT be used in production systems.
            // This Sample only exposes the entity sets needed by the application we are building.
            // This Sample uses EntitySetRight.All which allows both Read and Write access to the Entity Set.
            //config.SetEntitySetAccessRule("Arbitri", EntitySetRights.All);
            config.SetEntitySetAccessRule("*", EntitySetRights.All);
            //config.SetEntitySetAccessRule("AtributeJucatori", EntitySetRights.All);
            //config.SetEntitySetAccessRule("AtributeMeci", EntitySetRights.All);
            //config.SetEntitySetAccessRule("AtributeStatistici", EntitySetRights.All);
            //config.SetEntitySetAccessRule("Clasamente", EntitySetRights.All);
            //config.SetEntitySetAccessRule("Contracte", EntitySetRights.All);
            //config.SetEntitySetAccessRule("Echipe", EntitySetRights.All);
            //config.SetEntitySetAccessRule("EvolutieEchipe", EntitySetRights.All);
            //config.SetEntitySetAccessRule("FormatiiMeci", EntitySetRights.All);
            //config.SetEntitySetAccessRule("Jucatori", EntitySetRights.All);
            //config.SetEntitySetAccessRule("Meciuri", EntitySetRights.All);
            //config.SetEntitySetAccessRule("Stadioane", EntitySetRights.All);
            config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
        }
        [WebGet]
        public IEnumerable<ViewProgram> GetEtapeCampionat(String an)
        {           

            // Get the ObjectContext that is the data source for the service.
            FotbalEntities context = this.CurrentDataSource;

            try
            {

                var etapeC = (from etape in context.ViewPrograms
                             where etape.AnCampionat == an
                             select new 
                             {
                                 etape.etapa,
                                 etape.AnCampionat
                             }
                             ).Distinct();

                List<ViewProgram> list = etapeC.AsEnumerable()
                          .Select(o => new ViewProgram
                          {
                              etapa = o.etapa,
                              AnCampionat = o.AnCampionat
                          }).ToList();

                return list;
            }
            catch (Exception ex)
            {
                throw new ApplicationException(string.Format(
                    "An error occurred: {0}", ex.Message));
            }
        }
    }
}
